package com.wanshi.service;

import com.wanshi.dao.ProdDao;
import com.wanshi.entity.PageInfo;
import com.wanshi.entity.Product;
import com.wanshi.request.ProdReq;

import java.util.List;

public class ProdService {

    private ProdDao prodDao = new ProdDao();

    public void addProd(Product prod) {
        prodDao.addProd(prod);
    }

    public List<Product> findProdByUser(int id) {
        return prodDao.findProdByUser(id);
    }

    public void updateStateById(String id, String state) {
        prodDao.updateProdStateById(id,state);
    }

    public Product findProdById(String id) {
        return prodDao.findProdById(id);
    }

    public void updateProd(Product prod) {
        prodDao.updateProd(prod);
    }

    public PageInfo findByPageAndCond(ProdReq prodReq) {
        PageInfo pageInfo = new PageInfo();

        pageInfo.setSize(prodReq.getSize());

        Long total = prodDao.getCountByCond(prodReq.getName(),prodReq.getTypeid());
        pageInfo.setTotal(total);

        int page = prodReq.getPage();
        if(page > pageInfo.getLast()){
            page = pageInfo.getLast();
        }
        if(page < 1){
            page = 1;
        }
        pageInfo.setPage(page);

        List<Product> list = prodDao.findByPageAndCond( (page-1)*prodReq.getSize(),prodReq.getSize(),prodReq.getName(),prodReq.getTypeid());
        pageInfo.setList(list);
        return pageInfo;
    }
}
